package com.stardust.autojs.runtime.api;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.stardust.autojs.RootShell;
import com.stardust.autojs.core.inputevent.RootAutomator;
import com.stardust.autojs.runtime.ScriptRuntime;

import org.json.JSONObject;
import org.opencv.android.OpenCVLoader;

import java.io.File;
import java.util.Hashtable;
import java.util.Random;

/*
为脚本提供当前云控的相关功能
 */
public class Cloud {
    private ScriptRuntime mRuntime;
    private final Context mContext;
    private final String mToken;


    /**
     * @param token   当前设备的token
     * @param context 执行脚本的上下文
     */
    public Cloud(String token, Context context, ScriptRuntime runtime) {
        mContext = context;
        mToken = token;
        mRuntime = runtime;
    }

    public void setRuntime(ScriptRuntime runtime) {
        mRuntime = runtime;
    }

    public String test() {
        return "Success";
    }

    public Path getSnake(int x1, int y1, int x2, int y2, int count, int offset) throws Exception {
        return RootAutomator.getSnake(x1, y1, x2, y2, count, offset);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void snake1(int x1, int y1, int x2, int y2, int count, int offset, int dur) throws Exception {
        Path p = RootAutomator.getSnake(x1, y1, x2, y2, count, offset);
        mRuntime.automator.gesturePath(100, dur, p);
    }

    /**
     * 过微信滑块
     * <p>
     * public static  Point[] weixin007test(String path, Rect blockBounds, Rect bgBounds) throws Exception {
     * OpenCVLoader.initDebug();
     * Path p = TencentSlide.calculate(path, blockBounds, bgBounds);
     * Random random = new Random();
     * Point[] ps = pathToPoints(p, random.nextInt(20) + 100);
     * return ps;
     * }
     */

    public Point[] weixin007(String path, Rect blockBounds, Rect bgBounds) throws Exception {
        OpenCVLoader.initDebug();
        Point[] ps = TencentSlide.calculate(path, blockBounds, bgBounds);
        return ps;
    }


    /**
     * 解析二维码
     *
     * @param path
     * @return
     */
    public Result decodeqr(String path) {
        Bitmap scanBitmap;
        if (TextUtils.isEmpty(path)) {

            return null;

        }
        // DecodeHintType 和EncodeHintType
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 先获取原大小

        scanBitmap = BitmapFactory.decodeFile(path, options);
        options.inJustDecodeBounds = false; // 获取新的大小

        int sampleSize = (int) (options.outHeight / (float) 200);

        if (sampleSize <= 0)
            sampleSize = 1;
        options.inSampleSize = sampleSize;
        scanBitmap = BitmapFactory.decodeFile(path, options);
        if (scanBitmap == null) {
            return null;
        }
        // --------------测试的解析方法---PlanarYUVLuminanceSource-这几行代码对project没作功----------

        LuminanceSource source1 = new PlanarYUVLuminanceSource(
                rgb2YUV(scanBitmap), scanBitmap.getWidth(),
                scanBitmap.getHeight(), 0, 0, scanBitmap.getWidth(),
                scanBitmap.getHeight(), false);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
                source1));
        MultiFormatReader reader1 = new MultiFormatReader();
        Result result1;
        try {
            result1 = reader1.decode(binaryBitmap);
            return result1;
            // String content = result1.getText();
            // Log.i("123content", content);
        } catch (NotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
/*
        int[] pixs = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
        scanBitmap.getPixels(pixs, 0, 1, 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
        RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), pixs);
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        try {

            return reader.decode(bitmap1, hints);

        } catch (NotFoundException e) {

            e.printStackTrace();

        } catch (ChecksumException e) {

            e.printStackTrace();

        } catch (FormatException e) {

            e.printStackTrace();

        }
*/
        return null;

    }

}
